﻿using Ldt.DataEngine.RuleAssemble;
using LdtDataEngin.ModuleDetail.BaseModule;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Data;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel.SystemtableModel;
using LdtJudge.DataEngine.Model.ElmentModel;
using LdtJudge.DataEngine.Model.Personnel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CaseCauseElementExtr
{
    public class JudgeSuggestHandle: FactElementBaseClass
    {
        ///// <summary>
        ///// 数据交互
        ///// </summary>
        //public InteractiveShowClass interActives { get; set; }

        public override XmlNode ExtElementXmlParse(OnExtractComplete onExtract, NodeModelList nodeModelList)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 进行处理理由要素的抽取
        /// </summary>
        /// <param name="causeid"></param>
        /// <param name="personlisttext"></param>
        /// <returns></returns>
        public XmlDocument GetCauseJudgeXml(string causeid, Dictionary<PersonElementEntity, string> personlisttext,string casetype)
        {
            GetSysCaseCause dALSYS_CaseCause = new GetSysCaseCause();
            //causeid = "1658";
            XmlDocument xmlresult = new XmlDocument();
            xmlresult.LoadXml(string.Format("<{0}></{0}>", "理由要素"));
            ///1、进行对传入的人员信息，进行理由要素的匹配。
            base._dataContext.InteractiveShow.LogShow("\r\n     **************** 进行本院认为的理由要素内容抽取      开始    ****************");
            ///2、进行根据案由查询案由结构中配置了理由要素匹配规则的节点；
            List<SYS_CaseCause> dataTable = dALSYS_CaseCause.GetLoadCauseCurrentPersonFact(casetype,causeid);
            foreach (var personentiyinfo in personlisttext)
            {
                foreach (SYS_CaseCause row in dataTable)
                {
                    string ccid = row.CC_ID;
                    bool ismatch = RuleParseClass.IsMatchs(Convert.ToString(row.CC_RegexInfoCode), personentiyinfo.Value);
                    if (ismatch)
                    {///如果能够匹配上，则进行创建存储要素的节点。
                        XmlNode addnode = xmlresult.SelectSingleNode($"理由要素/具体要素[@要素ID='{Convert.ToString(row.CC_ParentID)}']");
                        if (addnode == null)
                        {///如果节点不存在就进行创建当前要素节点的要素
                            addnode = xmlresult.CreateElement("具体要素");
                            XmlAttribute attributet = xmlresult.CreateAttribute("要素ID");
                            attributet.Value = Convert.ToString(row.CC_ParentID);
                            addnode.Attributes.Append(attributet);
                            xmlresult.DocumentElement.AppendChild(addnode);
                        }
                        XmlNode addnodechild = addnode.SelectSingleNode($"主张方[@主张方姓名或名称='{personentiyinfo.Key.SP_Name}' and @主张方诉讼地位='{personentiyinfo.Key.SuitDegreeID}']");
                        if (addnodechild == null)
                        { ///2、进行对存储的节点配置添加
                            addnodechild = xmlresult.CreateElement("主张方");
                            addnode.AppendChild(addnodechild);
                            XmlAttribute attribute = xmlresult.CreateAttribute("主张方姓名或名称");
                            attribute.Value = personentiyinfo.Key.SP_Name;
                            addnodechild.Attributes.Append(attribute);
                            attribute = xmlresult.CreateAttribute("主张方诉讼地位");
                            attribute.Value = personentiyinfo.Key.SuitDegreeID;
                            addnodechild.Attributes.Append(attribute);
                        }

                        XmlNode nodezhuzhang = addnodechild.SelectSingleNode($"主张[@主张ID='{ Convert.ToString(row.CC_ID)}']");
                        if (nodezhuzhang == null)
                        {  ///3、添加主张XH的节点信息;
                            nodezhuzhang = xmlresult.CreateElement("主张");
                            addnodechild.AppendChild(nodezhuzhang);

                            XmlAttribute attribute = xmlresult.CreateAttribute("主张ID");
                            attribute.Value = Convert.ToString(row.CC_ID);
                            nodezhuzhang.Attributes.Append(attribute);
                            attribute = xmlresult.CreateAttribute("主张原文");
                            attribute.Value = Convert.ToString(row.CC_Name);
                            nodezhuzhang.Attributes.Append(attribute);
                        }


                    }
                }
            }
            base._dataContext.InteractiveShow.LogShow("\r\n" + xmlresult.OuterXml);
            base._dataContext.InteractiveShow.LogShow("\r\n     ****************  进行本院认为的理由要素内容抽取结束    ****************");
           
            return xmlresult;
        }
    }
}
